﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.Models;

namespace BlackBird.DataAccess
{
	public class EventsDataAccess
	{
		/// <summary>
		/// 插入收入支出单到Event表中去。
		/// </summary>
		/// <param name="inputDM"></param>
		/// <param name="idUser"></param>
		/// <returns></returns>
		public static int InsertNewEvent(IncomeAndExpendInputDM inputDM, string idUser)
		{
			using (var context = new blackbirdEntities())
			{
				string expectType = inputDM.type.ToString();
				var typeid = context.event_type.FirstOrDefault(v => v.Type == expectType).idEvent_type;
				string expectStatus = inputDM.Status.ToString();
				var statusid = context.status.FirstOrDefault(v => v.StatusType == expectStatus).idStatus;

				@event item = new @event()
				{
					Name = inputDM.Name,
					Count = inputDM.Count,
					Comments = inputDM.Description,
					Date = inputDM.Date,
					idProject = int.Parse(inputDM.ParentId),
					idUser = idUser,
					idStatus = statusid,
					idEventType = typeid
				};

				context.@event.AddObject(item);
				context.SaveChanges();

				return item.idEvent;
			}
		}

		/// <summary>
		/// Return event item by event_id
		/// event iclude: income,expend,event,jihua
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public static @event GetEventById(int id)
		{
			using (var context = new blackbirdEntities())
			{
				var item = from u in context.@event
							where u.idEvent == id
							select u;

				return item.First();
			}
		}

		public static int InsertEvent_to_Fujian(int eventid, int fujianid)
		{
			using (var context = new blackbirdEntities())
			{
				var event_fujian = new event_fujian()
				{
					Event_idEvent = eventid,
					Fujian_idFujian = fujianid
				};

				context.event_fujian.AddObject(event_fujian);

				context.SaveChanges();

				return event_fujian.id;
			}
		}

		/// <summary>
		/// Get all event items belong to this project.
		/// event include: income, expend, event.and planed events
		/// </summary>
		/// <param name="projectid"></param>
		/// <returns></returns>
		public static List<@event> GetAllEventsByProjectId(int projectid)
		{
			using (var context = new blackbirdEntities())
			{
				var users = from u in context.@event
							where u.idProject == projectid
							select u;

				return users.ToList();
			}
		}

		/// <summary>
		/// Return Events that not include the planed event.
		/// </summary>
		/// <param name="projectid"></param>
		/// <returns></returns>
		public static List<@event> GetAllEventsExceptPlanByProjectId(int projectid)
		{
			using (var context = new blackbirdEntities())
			{
				string expectType = SupportTypeStatus.JiHua.ToString();
				var jihuastatusid = context.status.FirstOrDefault(v => v.StatusType == expectType).idStatus;

				var retItems = from u in context.@event
							where u.idProject == projectid && u.idStatus != jihuastatusid
							select u;

				return retItems.ToList();

			}

		}
	}
}